﻿using System;
using Acme.Business;
using Acme.Common.Services;
using Acme.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Acme.MVVM.Test
{


    /// <summary>
    ///This is a test class for MainWindowViewModelTest and is intended
    ///to contain all MainWindowViewModelTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MainWindowViewModelFixture
    {

        TestContext _testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void SaveCommandTest()
        {

            //arrange
            var mockDialogService = new Mock<IDialogService>();
            var mockEventRepository = new Mock<IEventRepository>();
            var eventToSave = new Event() { Location = "Home", Title = "Christmas 2009", EventDate = new DateTime(2009, 12, 25) };

            mockEventRepository
                .Setup(er => er.Save(eventToSave))
                .Verifiable();

            var target = new MainWindowViewModel(mockEventRepository.Object, mockDialogService.Object);

            target.ActiveEvent = eventToSave;

            //act
            target.SaveCommand.Execute(null);

            //assert
            mockEventRepository.Verify();
            Assert.IsNotNull(target.ActiveEvent);
            Assert.AreSame(eventToSave, target.ActiveEvent);
        }

        [TestMethod()]
        public void ThrowCommandTest()
        {

            // Arrange
            var mockDialogService = new Mock<IDialogService>();
            var mockEventRepository = new Mock<IEventRepository>();

            mockDialogService
                .Setup(d => d.ShowException("testMessage", DialogImage.Error))
                .Verifiable();

            mockEventRepository
                .Setup(er => er.Save(null))
                .Throws(new ArgumentException("testMessage"));

            var target = new MainWindowViewModel(mockEventRepository.Object, mockDialogService.Object);

            target.ActiveEvent = new Event();

            // Act
            target.ThrowCommand.Execute(null);

            // Assert
            mockDialogService.Verify();

        }

        [TestMethod()]
        public void NewCommandTest()
        {

            // Arrange
            var mockDialogService = new Mock<IDialogService>();
            var mockEventRepository = new Mock<IEventRepository>();
            var eventToSave = new Event() { };

            mockEventRepository
               .Setup(er => er.Create())
               .Returns(eventToSave)
               .Verifiable();

            var target = new MainWindowViewModel(mockEventRepository.Object, mockDialogService.Object);

            target.SearchResultSelectedItem = new EventSearchResult(Guid.NewGuid(), "Title", DateTime.Today);

            // Act
            target.NewCommand.Execute(null);

            // Assert
            mockDialogService.Verify();

            Assert.IsNotNull(target.ActiveEvent);
            Assert.IsNull(target.SearchResultSelectedItem);
        }
    }
}
